package net.barbierdereuille.android.AndroBib;

import net.barbierdereuille.android.AndroBib.AndroBib.BibTexColumn;
import net.barbierdereuille.android.AndroBib.AndroBib.FieldColumn;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;

public class EntryViewActivity extends ListActivity {
	public final static String TAG = "EntryViewActivity";
	
	public static final String REFERENCE = "reference";
	
	private long mRefId;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "Creating EntryViewActivity activity");
        setContentView(R.layout.entry_view);
        ContentResolver content = getContentResolver();
        Bundle extras = getIntent().getExtras();
        mRefId = extras.getLong(REFERENCE);
        Log.d(TAG, "Fields for entry reference " + mRefId);
        
        String[] proj = {BibTexColumn.TYPE, BibTexColumn.KEY};
        Cursor ce = content.query(ContentUris.withAppendedId(BibTexColumn.CONTENT_URI, mRefId), proj,
        		BibTexColumn._ID + "=" + mRefId, null, null);
        ce.moveToFirst();
        TwoLineListItem key = (TwoLineListItem) findViewById(R.id.fieldKey);
        TextView type_view = (TextView)key.findViewById(android.R.id.text1);
        if(type_view != null)
        	type_view.setText(ce.getString(0));
        else {
        	Log.e(TAG, "Error, no type view found");
        }
        TextView key_view = (TextView)key.findViewById(android.R.id.text2);
        if(key_view != null)
        	key_view.setText(ce.getString(1));
        else {
        	Log.e(TAG, "Error, no key view found");
        }
        ce.close();
        
        Log.d(TAG, "type/key added");

    	String[] proj_fields = {FieldColumn._ID, FieldColumn.TYPE, FieldColumn.CONTENT};
    	Cursor c = content.query(FieldColumn.CONTENT_URI, proj_fields, FieldColumn.REFERENCE + "=" + mRefId, null, null);
    	startManagingCursor(c);
    	
    	String[] from_type = new String[] { FieldColumn.TYPE, FieldColumn.CONTENT };
    	int[] to_type = new int[] {android.R.id.text1, android.R.id.text2 };
    	ListAdapter keys = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, c, from_type, to_type);
    	setListAdapter(keys);
    }

}
